﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace nicotool
{
    public class nicovideo
    {
        /// <summary>
        /// 登陆cookie
        /// </summary>
        public static CookieContainer LoginCookie = new CookieContainer();
        /// <summary>
        /// 登陆nico
        /// </summary>
        /// <param name="username">帐号</param>
        /// <param name="passwd">密码</param>
        public static bool LoginToNicovideo(string username, string passwd)
        {
            //建立HttpWebRequest的实例
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(StrUrl.strLogin);
            if (nico_data.proxy != null)
            {

                req.Proxy = nico_data.proxy;
                 
            }
            
            req.Method = "POST"; //设置请求方式
            //mail=duo001z@gmail.com&password=420138wz
            string s = "mail="+username + "&" + "password="+passwd; //合并帐号密码为uri参数
            byte[] bs = Encoding.UTF8.GetBytes(s); //转为byte
            req.ContentLength = bs.Length; //设置 Content-lengthHTTP 标头
            req.ContentType = "application/x-www-form-urlencoded"; //设置 Content-typeHTTP 标头的值。
            req.CookieContainer = LoginCookie; //设置cookie
            req.GetRequestStream().Write(bs, 0, bs.Length); //发出请求

            string result;
            try
            {
                var resp = (HttpWebResponse)req.GetResponse();
                
                result = new StreamReader(resp.GetResponseStream()).ReadToEnd();
            }
            catch (Exception ex)
            {

                throw new Exception("Error during login proccess.", ex);
                
            }

            //FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory+ "\\c.txt",FileMode.Create);
            //StreamWriter sw = new  StreamWriter(fs);
            //sw.WriteLine(result);
            //sw.Close();
            //fs.Close();
           
            if (result.Contains("wrongPass"))
            {
               
                return false;
            }
            return true;

        }

        /// <summary>
        /// 指定动画的cookie获得
        /// </summary>
        /// <param name="id">动画id</param>
        /// <returns>动画cookie</returns>
        private static CookieCollection GetWatchCookie(string id)
        {
            var req = (HttpWebRequest)HttpWebRequest.Create(StrUrl.getSmUrl(id));
            if (nico_data.proxy != null)
            {

                req.Proxy = nico_data.proxy;
            }
            
            req.CookieContainer = LoginCookie;
            HttpWebResponse resp;
            try
            {
                resp = (HttpWebResponse)req.GetResponse();
            }
            catch (Exception ex)
            {
                
                throw new Exception("cookie 获得失败",ex);
            }

            return resp.Cookies; ;
        }

        public static CustomWebClient GetWebClientForFlv(string id)
        {
            var client = new CustomWebClient();
            client.CookieContainer.Add(GetWatchCookie(id));
            return client;

        }
    }
}
